package com.zuoye.day19;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class ProductTest {
    public static void main(String[] args) {

        //（1）添加若干个商品对象
        List<Product> list = new ArrayList<>();
        Product product1 = new Product("1","农夫山泉",2,"江苏", LocalDateTime.of(2022,8,3,13,23,12));
        Product product2 = new Product("2","荔枝",34,"江苏", LocalDateTime.of(2022,8,11,8,10,2));
        Product product3 = new Product("3","杯子",2335,"上海", LocalDateTime.of(2002,7,1,6,3,34));
        Product product4 = new Product("4","风扇",30,"江苏", LocalDateTime.of(2022,8,1,4,12,4));
        Product product5 = new Product("5","水杯",3333,"苏州", LocalDateTime.of(2007,8,3,13,23,12));
        Product product6 = new Product("6","荔枝1",34,"江苏", LocalDateTime.of(2022,8,11,8,10,2));
        Product product7 = new Product("7","农夫山泉1",2,"苏州", LocalDateTime.of(2022,8,3,13,23,12));
        Product product8 = new Product("8","荔枝2",34,"上海", LocalDateTime.of(2022,8,11,8,10,2));
        list.add(product1);
        list.add(product2);
        list.add(product3);
        list.add(product4);
        list.add(product5);
        list.add(product6);
        list.add(product7);
        list.add(product8);
        //System.out.println(list);

        System.out.println("获取第3个商品对象: " + list.get(2));
        System.out.println("删除第2个商品对象: " + list.remove(1));

        System.out.println("价格改为原来的1.5倍" + list.get(2).getProPrice()*1.5);

        System.out.println("迭代器遍历该集合中的所有商品对象:");
        Iterator<Product> it = list.iterator();
        while (it.hasNext()){
            Product ele = it.next();
            System.out.println(ele);
        }

        System.out.println("使用foreach遍历该集合中的所有商品对象:");
        for (Product product : list){
            System.out.println(product);
        }

        System.out.println("价格超过300的商品，将这些商品放置另一个集合ArrayList中:");
        for (Product product : list){
            if (product.getProPrice() > 300)
                System.out.println(product);
        }

        System.out.println("统计出个产地商品的数量（如 \"苏州\"-5, \"上海\"-10,\"杭州\"-6）");
       Map<String,Integer> proMap = new HashMap<>();
        for (Product product :list){
            String proCity = product.getProCity();
            if (proMap.containsKey(proCity)){
                proMap.put(product.getProCity(), proMap.get(product.getProCity())+1);
            }else {
                proMap.put(product.getProCity(),1);
            }
        }

        for (String str : proMap.keySet()){
            System.out.println(str + "-" + proMap.get(str));
        }



        System.out.println("找出生产日期在\"2009-10-1\"前的商品，放置另一个集合ArrayList中:");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (Product product : list){
            if (product.getLocalDateTime().toLocalDate().isBefore((LocalDate.parse("2009-10-01",dateTimeFormatter))))
            System.out.println(product);
        }







}
}